package com.gz.logistics.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.logistics.R;
import com.gz.logistics.app.App;
import com.gz.logistics.app.Define;
import com.gz.logistics.app.SelectPayTypeActivity;
import com.gz.logistics.library.IAuthor;
import com.gz.logistics.library.util.Constants;
import com.gz.logistics.util.NetworkUtil;
import com.gz.logistics.util.ShareUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

	private IWXAPI api;
	private TextView pay_result;
	private Button button;//完成按钮

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wx_payresult);

		api = WXAPIFactory.createWXAPI(this,"wx38699b5a87253a8b");
		api.handleIntent(getIntent(), this);

		pay_result = (TextView) findViewById(R.id.pay_result);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();
			}
		});

	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.i("chenghao", "onPayFinish, errCode =" + resp.errCode);
		Message msg=new Message();
		msg.arg1=resp.errCode;
		msg.what=1000;
		myhandler.sendMessage(msg);


	}

	Handler myhandler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			int type=msg.arg1;
//			pay_result.setText("?????fanhui"+type);
			//Toast.makeText(WXPayEntryActivity.this,"返回="+resp.errCode+";str="+resp.errStr,Toast.LENGTH_LONG).show();
			if (type==0){//支付成功
			/*Intent broastIntent = new Intent("successpay");
			sendBroadcast(broastIntent);*/
				pay_result.setText("支付成功");
				Wechatresult();
			}else{
				pay_result.setText("支付失败");
			}


		}
	};

	public void Wechatresult() {
		if (NetworkUtil.isNetworkConnected(WXPayEntryActivity.this)) {
			String url = Constants.URL_HOST+"order_query";
			RequestParams params = new RequestParams(url);
			params.addParameter("out_trade_no", App.pay_sn);
			params.addParameter("receiveType",App.pay_wechat_type);
			params.addParameter("uid", ShareUtil.getstringvalue(WXPayEntryActivity.this,"uid") );
			x.http().get(params, new Callback.CommonCallback<String>() {
				@Override
				public void onCancelled(CancelledException arg0) {
				}

				@Override
				public void onError(Throwable arg0, boolean arg1) {
				}

				@Override
				public void onFinished() {
				}

				@Override
				public void onSuccess(String result) {
					try {
						JSONObject json = new JSONObject(result.toString());
						if ("0".equals(json.getString("ret"))){

						}else{
							Toast.makeText(WXPayEntryActivity.this,"支付成功,数据操作失败,请联系客服处理",Toast.LENGTH_LONG).show();
						}
						finish();
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}
			});
		} else {
		}
	}
}